import logging, config, os
#from logging.handlers import RotatingFileHandler


def setup_logging(logger_name="messages", log_filename="messages.log", loglevel=logging.DEBUG, overwrite=True):
    # create logger with logger_name"
    logger = logging.getLogger(logger_name)
    logger.setLevel(loglevel)
    logdir = os.path.abspath(config.LOG_FOLDER)
    log = os.path.join(logdir, log_filename)
    try:
        if overwrite and os.path.exists(log):
            os.remove(log)
    except: pass    # create file handler which logs even debug messages
    fh = logging.FileHandler(log)
    #fh.setLevel(logging.DEBUG)
    # create console handler with a higher log level
    #ch = logging.StreamHandler()
    #ch.setLevel(logging.ERROR)
    # create formatter and add it to the handlers
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    fh.setFormatter(formatter)
    #ch.setFormatter(formatter)
    # add the handlers to the logger
    logger.addHandler(fh)
    #logger.addHandler(ch)
    return logger

